﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Home.Utils;
using NhanAi.DAO;
using NhanAi.DAO.Mix;
using NhanAi.Entity;
using NhanAi.App_Code;
namespace NhanAi.Admin
{
    public partial class CreatePage : BasePage
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            Authenticate();
           
            cmbStatus.Items.Add(new ListItem("Nháp", NAConstants.POST_STA_DRAFT.ToString()));
            cmbStatus.Items.Add(new ListItem("Cho phép đăng", NAConstants.POST_STA_POST.ToString()));

            SetCurrentPage("Page");
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            PostModel dao = new PostModel();
            Post p = new Post();
            bool rs = false;
            try
            {                
                p.Title = txtTitle.Text;
                p.TitleEn = txtTitleEn.Text;
                p.Content = PostContent.Value;
                p.ContentEn = PostContentEn.Value;
                p.Status = int.Parse(cmbStatus.SelectedItem.Value);
               
                p.PostDate = DateTime.Now;
                p.ModifiedDate = DateTime.Now;
                p.MenuOrder = 0;
                p.Protected = chkProtected.Checked;
                p.Guid = "";
              
                p.PostType = "page";
                p.Excerpt = StringUltility.GetExcerpt(p.Content,NAConstants.EXCERPT_LEN);
                p.ExcerptEn = StringUltility.GetExcerpt(p.ContentEn,NAConstants.EXCERPT_LEN);
                p.Name = txtName.Text;

               // if (p.Excerpt.Length > NAConstants.EXCERPT_LEN) p.Excerpt.Substring(0, NAConstants.EXCERPT_LEN);
                //if (p.ExcerptEn.Length > NAConstants.EXCERPT_LEN) p.ExcerptEn.Substring(0, NAConstants.EXCERPT_LEN);
                if (Session["user"] != null)
                {
                    User u = (User)Session["user"];
                    p.AuthorId = u.Id;
                }
               
                dao.Create(p);
                SetMessage("Thêm mới thành công", "");
                rs = true;
            }
            catch (Exception ex)
            {
                SetMessage("Đã xảy ra lỗi","");
            }
            
            if(rs)Redirect("/Admin/EditPage.aspx?id=" + p.Id);
        }
       
    }
}